package com.jty.module_user.ui.fragment

import android.text.InputFilter
import android.text.InputType
import android.text.Spanned
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.lib_base.config.AppConstants
import com.jty.lib_base.base.BaseFragment
import com.jty.lib_base.widget.EditTextMonitor
import com.jty.module_user.BR
import com.jty.module_user.R
import com.jty.module_user.databinding.UserFragmentModifyPwdBinding
import com.jty.module_user.viewmodel.ModifyPwdViewModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.subjects.PublishSubject

@Route(path = AppConstants.Router.User.F_MODIFYPWD)
class UserModifyPwdFragment : BaseFragment<UserFragmentModifyPwdBinding, ModifyPwdViewModel>() {

    override fun initContentView(): Int {
        return R.layout.user_fragment_modify_pwd
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        super.initData()

        val oldPassSubject = PublishSubject.create<String>()
        val newPassSubject = PublishSubject.create<String>()
        val rePassSubject = PublishSubject.create<String>()
        binding.etOldpwd.addTextChangedListener(EditTextMonitor(oldPassSubject))
        binding.etNewpwd.addTextChangedListener(EditTextMonitor(newPassSubject))
        binding.etAgainpwd.addTextChangedListener(EditTextMonitor(rePassSubject))

        viewModel.addSubscribe(io.reactivex.Observable.combineLatest(
            oldPassSubject,
            newPassSubject,
            rePassSubject
        ) { account: String, pwd: String, code:String->
            account.isNotBlank() && pwd.isNotBlank() && code.isNotBlank()
        }.observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                binding.btnModify.isEnabled = it
                binding.btnModify.setBackgroundResource(if (it) R.drawable.shape_round_white else R.drawable.green_btn_corner_10dp)
            }
        )

        val filter = object : InputFilter{
            override fun filter(
                p0: CharSequence?,
                p1: Int,
                p2: Int,
                p3: Spanned?,
                p4: Int,
                p5: Int
            ): CharSequence {
                return p0?.let{if(it.equals(" ")) "" else it}?:""
            }

        }
       binding.etOldpwd.filters = arrayOf(filter, InputFilter.LengthFilter(12))
       binding.etNewpwd.filters = arrayOf(filter, InputFilter.LengthFilter(12))
       binding.etAgainpwd.filters = arrayOf(filter, InputFilter.LengthFilter(12))
    }

    override fun initViewObservable() {
        viewModel.uc.changeEyeStateOld.observe(this){
            binding.ivEyeOld.setImageResource(if(it)R.drawable.ic_eye else R.drawable.ic_eye_hide)
            if (it){
                binding.etOldpwd.transformationMethod = HideReturnsTransformationMethod.getInstance()
            }else{
                binding.etOldpwd.transformationMethod = PasswordTransformationMethod.getInstance()
            }
        }

        viewModel.uc.changeEyeStateNew1.observe(this){
            binding.ivEyeNew1.setImageResource(if(it)R.drawable.ic_eye else R.drawable.ic_eye_hide)
            if (it){
                binding.etNewpwd.transformationMethod = HideReturnsTransformationMethod.getInstance()
            }else{
                binding.etNewpwd.transformationMethod = PasswordTransformationMethod.getInstance()
            }
        }

        viewModel.uc.changeEyeStateNew2.observe(this){
            binding.ivEyeNew2.setImageResource(if(it)R.drawable.ic_eye else R.drawable.ic_eye_hide)
            if (it){
                binding.etAgainpwd.transformationMethod = HideReturnsTransformationMethod.getInstance()
            }else{
                binding.etAgainpwd.transformationMethod = PasswordTransformationMethod.getInstance()
            }
        }
    }
}